﻿var Utils = new Object();
var ajaxDefault = {
    url: null,
    data: {},
    type: 'POST',
    success: null,
    error: null,
    before: null
};
Utils.ajaxClassify = null;
Utils.ajax = function (opts) {
    opts = $.extend({}, ajaxDefault, opts || {});
    if (Utils.ajaxClassify != null)
        opts.data.Classify = Utils.ajaxClassify;
    $.ajax({
        url: opts.url,
        data: opts.data,
        type: opts.type,
        dataType: 'json',
        beforeSend: function () {
            if (opts.before)
                opts.before();
        },
        success: function (d) {
            if (opts.success)
                opts.success(d);
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            if (opts.error)
                opts.error(textStatus);
            else
                alert('数据传输错误，请重试！');
        }
    });
};

Utils.setObject2Dom = function (obj) {
    for (var domID in obj) {
        $('#' + domID).val(obj[domID]);
    }
};

Utils.confirm = function (title, content, okCallback, cancelCallback) {
    var msg = $('<div>' + content + '</div>');
    msg.dialog({
        title: title ? title : '请确认',
        width: 300,
        height: 200,
        modal: true,
        buttons: [{
            text: '确定',
            click: function () {
                if (okCallback) {
                    okCallback();
                }
                $(this).dialog("close");
            }
        }, {
            text: '取消',
            click: function () {
                if (cancelCallback) {
                    cancelCallback();
                }
                $(this).dialog("close");
            }
        }]
    });
};

Utils.alert = function (title, content, okCallback) {
    var msg = $('<div>' + content + '</div>');
    msg.dialog({
        title: title ? title : '请确认',
        width: 300,
        height: 200,
        modal: true,
        buttons: [{
            text: '确定',
            click: function () {
                if (okCallback) {
                    okCallback();
                }
                $(this).dialog("close");
            }
        }]
    });
}

Utils.checkAll = function (checked) {
    if (checked)
        $('.cboItem').attr('checked', 'checked');
    else
        $('.cboItem').removeAttr('checked');
}